package com.onlyforfun.exchange.dao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.onlyforfun.exchange.R;

public class SortKeeper {
	private static final String PREFERENCES_NAME = "com_forfun_exchange_pref_sortkey";

	public static void keepBank(Context context, int bank) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
		Editor editor = pref.edit();
		editor.putInt("bank" , bank);
		editor.commit();
	}
	
	public static int readBank(Context context) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
		return pref.getInt("bank" , 0);
	}
	public static void keepSortkeys(Context context, List<String> sortkeys) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
		Editor editor = pref.edit();
		editor.putInt("keylength", sortkeys.size());
		for (int i = 0; i < sortkeys.size(); i++) {
			editor.putString("sortkey_" + i, sortkeys.get(i));
		}
		editor.commit();
	}

	public static void clear(Context context) {
	    SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
	    Editor editor = pref.edit();
	    editor.clear();
	    editor.commit();
	}

	public static List<String> readSortkeys(Context context) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
		if (!pref.contains("keylength")) {;
			return new ArrayList<String>(Arrays.asList(context.getApplicationContext().getResources().getStringArray(R.array.CurrencyName)));
		}
		
		List<String> sortkeys = new ArrayList<String>();
		int len =  pref.getInt("keylength", 0);
		for (int i = 0; i < len; i++) {
			sortkeys.add(pref.getString("sortkey_" + i, ""));
		}
		return sortkeys;
	}
}
